package com.itcast.googleplay;

import java.util.ArrayList;

import uk.co.senab.photoview.PhotoView;

import com.itcast.googleplay.http.HttpApi;
import com.itcast.googleplay.utils.UIUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;

public class PicActivity extends Activity {
	private ArrayList<String> imgList;
	private DisplayImageOptions options;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		setContentView(R.layout.activity_pic);
		options = new DisplayImageOptions.Builder()
			.showImageOnLoading(R.drawable.ic_default)
			.showImageForEmptyUri(R.drawable.ic_default)
			.showImageOnFail(R.drawable.ic_default)
			.bitmapConfig(Bitmap.Config.RGB_565)
			.cacheInMemory(true)
			.cacheOnDisk(true)
			.considerExifParams(true)//是否允许让图片反转
			.build();
		
		imgList = getIntent().getStringArrayListExtra("imgList");
		
		ViewPager viewpager = (ViewPager) findViewById(R.id.viewpager);
		MyPagerAdapter myPagerAdapter = new MyPagerAdapter();
		viewpager.setAdapter(myPagerAdapter);
	}
	
	class MyPagerAdapter extends PagerAdapter{
		@Override
		public int getCount() {
			return imgList.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
//			ImageView imageView = new ImageView(UIUtils.getContext());
			PhotoView photoView = new PhotoView(UIUtils.getContext());
			ImageLoader.getInstance().displayImage(HttpApi.IMAGEURL+imgList.get(position), photoView,options);
			container.addView(photoView);
			return photoView;
		}
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View)object);
		}
	}
}
